#ifndef __MYSQLCLIENT_H__
#define __MYSQLCLIENT_H__

#include <string>
#include <vector>
#include <mysql/mysql.h>

using std::string;
using std::vector;

namespace myNameSpace
{
    class MySQLClient
    {
    public:
        MySQLClient();
        ~MySQLClient();

        void init();
        void connect(const string &host,
                     const string &user,
                     const string &password,
                     const string &database,
                     unsigned short port);

        bool writeOperationQuery(const string &sql);
        vector<vector<string>> readOperationQuery(const string &sql);

        bool appendData(const string &tableName,
                        const string &dataFiled,
                        const string &data,
                        const string &keyFiled,
                        const string &key);
        // 该函数用于向表tableName中，keyFiled=key的这一行，dataFiled列，添加数据data
        // 如果data和key在原来的SQL语法中是字符类型，需要手动在其前后添加
        // 即实际传入 "'" + data + "'"

        string findData(const string &tableName,
                        const string &dataFiled,
                        const string &keyFiled,
                        const string &key);
        // 该函数用于在表tableName中，keyFiled=key的这一行，查询其dataFiled列
        // 如果key在原来的SQL语法中是字符类型，需要手动在其前后添加
        // 即实际传入 "'" + data + "'"
        void MySQLClient::dump(const vector<vector<string>> &res)
        {
            cout << "result set has " << res.size() << " row(s)" << endl;
            for (auto &vec : res)
            {
                for (auto &filed : vec)
                {
                    cout << filed << "\t";
                }
                cout << endl;
            }
            cout << endl;
        }

    private:
        MYSQL _mysqlConnection;
    };
} // end of namespace myNameSpace

#endif
